﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Dycox.Validation
{
    public abstract class ValidatorAttribute : Attribute
    {
        protected ValidatorAttribute(params object[] parameters)
        {
            _parameters = parameters;
        }

        private object[] _parameters;

        protected abstract Validator CreateValidator(object[] parameters, ITypeDescriptorContext context);

        public Validator CreateValidator(ITypeDescriptorContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            var validator = CreateValidator(_parameters, context);
            if (_errorMessage != null) validator.ErrorMessage = _errorMessage;
            return validator;
        }

        private string _errorMessage;
        /// <summary>
        /// Gets or sets the message when validation fails.
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        private string _condition;
        /// <summary>
        /// Gets or sets an C# syntax expression to determine whether to validate object.
        /// <c>null</c> for all groups.
        /// </summary>
        public string Condition
        {
            get { return _condition; }
            set { _condition = value; }
        }

        private bool _isWarning;
        /// <summary>
        /// Gets or sets a value indicates whether to treat the validation failure as warning.
        /// </summary>
        public bool IsWarning
        {
            get { return _isWarning; }
            set { _isWarning = value; }
        }

    }
}
